// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "SnakeBase.generated.h"

UENUM()
enum class EMovementDirection
{
	UP,
	DOWN,
	LEFT,
	RIGHT
};

DECLARE_DYNAMIC_MULTICAST_DELEGATE(FOnSnakeDestroyed);

UCLASS()
class SNAKE_API ASnakeBase : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	ASnakeBase();

	UPROPERTY(EditDefaultsOnly);
	TSubclassOf<class ASnakeElementBase> SnakeElementClass;

	UPROPERTY(EditDefaultsOnly);
	float MovementSpeed = 0.125f;

	UPROPERTY(EditDefaultsOnly);
	float MovementStep = 100.f;

	UPROPERTY()
	TArray<ASnakeElementBase*> SnakeElements;
	
	UPROPERTY(BlueprintAssignable)
	FOnSnakeDestroyed OnSnakeDestroyedDelegate;

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

	UPROPERTY(EditDefaultsOnly)
	EMovementDirection CurrentMovementDirection = EMovementDirection::DOWN;

	UPROPERTY()
	bool IsRuned = false;
	
	UPROPERTY()
	bool CanSetMovementDirection = false;

	UPROPERTY()
	bool IsRequiredAddNewElement = false;

	UPROPERTY()
	bool IsRequiredTeleport = false;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;
	
	UFUNCTION(BlueprintCallable)
	void CreateSnake();

	UFUNCTION(BlueprintCallable)
	void Run();

	UFUNCTION(BlueprintCallable)
	void AddSnakeElement(FVector Position);

	UFUNCTION(BlueprintCallable)
	void SetSnakeElementAddRequired();

	UFUNCTION(BlueprintCallable)
	void SetSnakeTeleportRequired();

	UFUNCTION(BlueprintCallable)
	void Move();

	UFUNCTION()
	EMovementDirection GetCurrentMovementDirection();

	UFUNCTION()
	void SetMovementDirection(EMovementDirection MovementDirection);

	UFUNCTION()
	void SnakeElementOverlap(ASnakeElementBase* OverlapElement, AActor* OtherActor);

	UFUNCTION()
	TArray<ASnakeElementBase*> GetElements();

	UFUNCTION()
	void HandleSnakeDestroyed(AActor* DestroyedActor);
};
